#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <unistd.h>
#include <string>

size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
    size_t real_size = size * nmemb;
    std::string data = (char*)ptr;
    printf("\n\n#############################data: \n%s\n", data.c_str());
    return real_size;
}


int main(void)
{
    CURL *curl;
    CURLcode res;

    curl_global_init(CURL_GLOBAL_ALL);
    /* get a curl handle */
    curl = curl_easy_init();

    if (!curl) {
        return -1;
    }

    /*设置easy handle属性*/
    /* specify URL */
    char* url = "https://localhost:8999";
    curl_easy_setopt(curl, CURLOPT_URL, url);

    /* specify we want to POST data */
    curl_easy_setopt(curl, CURLOPT_POST, 1L);
    /* Set the expected POST size */
    size_t datelen = strlen(url);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)datelen);
    /* Set the expected POST data */
    char* postdata = "123689";
    curl_easy_setopt(curl,CURLOPT_POSTFIELDS, (char *)postdata);

    curl_easy_setopt (curl,CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt (curl,CURLOPT_SSL_VERIFYHOST, 0L);
    // curl_easy_setopt (curl,CURLOPT_SSLCERT,"client.crt");
    curl_easy_setopt (curl, CURLOPT_SSLCERTTYPE, "PEM");
    // curl_easy_setopt (curl, CURLOPT_SSLKEY,"client.key");
    curl_easy_setopt (curl, CURLOPT_SSLKEYTYPE,"PEM");
    curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt (curl,CURLOPT_TIMEOUT, 60L);
    curl_easy_setopt (curl,CURLOPT_CONNECTTIMEOUT, 10L);

    /*执行数据请求*/
    printf("curl: %s\n", url);
    while(true)
    {
        res = curl_easy_perform(curl);
        if(res !=CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));
        }
        sleep(1);
    }

    // 释放资源
    curl_easy_cleanup(curl);
    curl_global_cleanup();

    return 0;
}
